package com.wash.shoes.controller.app;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.StringUtils;
import com.wash.shoes.entity.BrandPo;
import com.wash.shoes.entity.FactoryAreaConfigPo;
import com.wash.shoes.request.BrandQueryRequest;
import com.wash.shoes.request.BrandWithFactoryIdQueryRequest;
import com.wash.shoes.resp.BrandVo;
import com.wash.shoes.service.BrandService;
import com.wash.shoes.service.FactoryAreaConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

@Api(tags = {"APP-品牌"})
@RestController
@RequestMapping("/app/brand")
public class AppBrandController {
    @Autowired
    private BrandService brandService;

    @Autowired
    private FactoryAreaConfigService factoryAreaConfigService;

    @ApiOperation("根据工厂id获取覆盖的品牌列表")
    @GetMapping("/list")
    public R<List<BrandVo>> pageList(@Valid BrandWithFactoryIdQueryRequest brandWithFactoryIdQueryRequest) {
        Set<String> brandIdSet1 = null;
        if (Objects.nonNull(brandWithFactoryIdQueryRequest.getPlatformType())) {
            //根据渠道查品牌
            List<BrandPo> brandPos = brandService.list(new LambdaQueryWrapper<BrandPo>().eq(BrandPo::getStatus, 1).eq(BrandPo::getPlatformType, brandWithFactoryIdQueryRequest.getPlatformType()));
            if (CollectionUtil.isNotEmpty(brandPos)) {
                brandIdSet1 = brandPos.stream().map(BrandPo::getId).collect(Collectors.toSet());
            }
        }

        List<FactoryAreaConfigPo> factoryAreaConfigPoList = factoryAreaConfigService.list(new LambdaQueryWrapper<FactoryAreaConfigPo>()
                .eq(FactoryAreaConfigPo::getFactoryId, brandWithFactoryIdQueryRequest.getFactoryId()).
                in(CollectionUtil.isNotEmpty(brandIdSet1), FactoryAreaConfigPo::getBrandId, brandIdSet1)
                .eq(FactoryAreaConfigPo::getStatus, 1));

        List<BrandVo> data = Lists.newArrayList();
        Set<String> brandIdSet = factoryAreaConfigPoList.stream().map(FactoryAreaConfigPo::getBrandId).collect(Collectors.toSet());
        if (CollectionUtil.isNotEmpty(brandIdSet)) {
            List<BrandPo> page = brandService.list(
                    new LambdaQueryWrapper<BrandPo>().in(CollectionUtil.isNotEmpty(brandIdSet), BrandPo::getId, brandIdSet).eq(BrandPo::getStatus, 1)
            );
            data = page.stream().map(item -> {
                BrandVo brandVo = new BrandVo();
                brandVo.setBrandId(item.getId());
                brandVo.setBrandName(item.getBrandName());
                return brandVo;
            }).collect(Collectors.toList());
        }

        return R.ok(data);
    }


    @ApiOperation("获取列表")
    @PostMapping("/list1")
    public R<Page<BrandPo>> pageList(@RequestBody @Valid BrandQueryRequest bannerPo) {
        Page<BrandPo> page = brandService.page(bannerPo.toPage(),
                new LambdaQueryWrapper<BrandPo>()
                        .like(StringUtils.isNotEmpty(bannerPo.getKeywords()), BrandPo::getBrandName, bannerPo.getKeywords())
                        .or().like(StringUtils.isNotEmpty(bannerPo.getKeywords()), BrandPo::getAddress, bannerPo.getKeywords())
                        .eq(BrandPo::getStatus, 1).eq(StringUtils.isNotBlank(bannerPo.getPlatformType()), BrandPo::getPlatformType, bannerPo.getPlatformType())
        );

        return R.ok(page);
    }
}
